// pages/mine/mine.js
const { $Toast } = require('../../dist/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:{},
    money:0,
    isLogin:false,
    touxiang:"",
    userChange:false
  },

  click(e){
    let num=e.currentTarget.dataset['index']
    console.log('......',num)
    switch (num) {
      case "1":
        wx.showToast({
          title: '抱歉，此功能暂未开放',
          icon: 'none',
          duration: 2000
        })
        
        break;
      case "2":
        wx.navigateTo({
          url: '../mine-item2/mine-item2-two/two',
          success(res) {
            
          }
        })
        break;
      case "3":
        wx.navigateTo({
          url: '../mine-item2/mine-item2-three/three',
          success(res) {
            
          }
        })
        break;
      case "4":
        wx.showToast({
          title: '抱歉，此功能暂未开放',
          icon: 'none',
          duration: 2000
        })
        break;
      default:
        wx.navigateTo({
          url: '../mine-item2/mine-item2-five/five',
          success(res) {
            
          }
        })
        break;
    }
  },
 
    handleclick(e){
      let num=e.currentTarget.dataset['index']
      console.log('......',num)
      switch (num) {
        case "1":
          wx.navigateTo({
            url: '../mine-item/mine-item-baobei/baobei',
            success(res) {
              
            }
          })
          
          break;
        case "2":
          wx.navigateTo({
            url: '../mine-item/mine-item-youhui/youhui',
            success(res) {
              
            }
          })
          break;
        case "3":
          wx.navigateTo({
            url: '../mine-item/mine-item-address/address',
            success(res) {
              
            }
          })
          break;
        default:
          wx.showToast({
            title: '抱歉，此功能暂未开放',
            icon: 'none',
            duration: 2000
          })
          break;
      }
    },

  chonzhi(){
    wx.navigateTo({
      url: '../chonzhi/chonzhi',
    })
  },

  tixian(){
    wx.navigateTo({
      url: '../tixian/tixian',
    })
  },
  
  layout(){
    var app = getApp();
    var baseurl = app.globalData.BaseUrl;
    var token = app.globalData.token;
    var that = this;
    wx.showModal({
      title: '确认退出',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: baseurl+'/api/user/layout',
            header:{
              'content-type': 'application/x-www-form-urlencoded', // 默认值
              'token':token
            },
            method:'DELETE',
            success:(res)=>{
              //用户数据修改
              if(res.data.code == 0){
                app.globalData.token = "";
                that.onShow()
              }else{
                console.log("失败")
              }
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })
    
  },

  tologin(){
    wx.navigateTo({
      url: '../login/login',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var app = getApp();
    var baseurl = app.globalData.BaseUrl;
    var token = app.globalData.token;
    var user = app.globalData.userInfo;
    var userChange = app.globalData.userChange;
    var that = this;
    if(token != ""){
      this.setData({
        user:user,
        money:user.money.toFixed(1),
        isLogin:true,
        touxiang:baseurl+"/img/"+user.img
      })
    }else{
      this.setData({
        isLogin:false
      })
    }
    if(userChange == true){
      //重新获取用户数据
      wx.request({
        url: baseurl+'/api/user/'+user.id,
        header:{
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'token':token
        },
        method:'GET',
        success:(res)=>{       
          if(res.data.code == 0){
            app.globalData.userChange = false;
            app.globalData.userInfo = res.data.data;
            that.onShow()
          }
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})